package com.halflife.comeElectricity.basic.datadic;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.user.UserView;
import com.halflife.framework.utils.Utils;
import com.halflife.shiro.utils.ShiroUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class DepositAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private DepositRepository depositRepository;
    @Resource
    private RentTypeRepository rentTypeRepositoty;

    @RequestMapping("/deposit/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        UserView user = ShiroUtil.getUserView(request);
        queryParam.put("departmentId", user.getDepartmentId());
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = depositRepository.selectCount(queryParam);
        List<Deposit> list = depositRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "deposit/list";
    }

    @RequestMapping("/deposit/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        Deposit deposit = new Deposit();
        if(id!=null){
            deposit = depositRepository.selectByPrimaryKey(id);
        }else{
            UserView userview = ShiroUtil.getUserView(request);
            deposit.setDepartmentId(userview.getDepartmentId());
            deposit.setDepartment(userview.getDepartment());
        }
        Map<String, Object> param = new HashMap<>();
        param.put("valie", true);
        List<RentType> rentTypeList = rentTypeRepositoty.selectAll(param);

        model.addAttribute("rentTypeList",rentTypeList);
        model.addAttribute("deposit",deposit);
        return "deposit/operate";
    }

    @PostMapping("/deposit/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(Deposit deposit, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            deposit.setCatagory(Utils.getString(deposit.getRentTypeId()));
            if(deposit.getId()!=null){
                depositRepository.updateByPrimaryKey(deposit);
            }else {
                deposit.init();
                Integer a = depositRepository.insert(deposit);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/deposit/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            Deposit deposit = new Deposit();
            deposit.setId(id);
            deposit.setValid(Boolean.FALSE);
            depositRepository.updateByPrimaryKeySelective(deposit);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
